﻿//-----------------------------------------------------------------------
// <copyright file="ShowUserNamePasswordPromptCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.UserNamePassword
{
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.UI.ViewModels;
    using ChatBox.Utility;

    /// <summary>
    /// Command to show the UserNamePasswordPrompt window
    /// </summary>
    public class ShowUserNamePasswordPromptCommand : GenericCommandBase<WindowViewModel>
    {
        /// <summary>
        /// The source of server settings
        /// </summary>
        private readonly IServerSettingsRepository serverSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the ShowUserNamePasswordPromptCommand class
        /// </summary>
        /// <param name="serverSettingsRepository">The source of server settings</param>
        public ShowUserNamePasswordPromptCommand(IServerSettingsRepository serverSettingsRepository)
        {
            this.serverSettingsRepository = serverSettingsRepository;
        }

        /// <summary>
        /// Execure the command
        /// </summary>
        /// <param name="value">the parent window</param>
        public override void ExecuteOnValue(WindowViewModel value)
        {
            UserNamePasswordPrompt promptWindow = new UserNamePasswordPrompt();
            UserNamePasswordViewModel viewModel = Locator.Resolve<UserNamePasswordViewModel>();

            this.Populate(promptWindow, viewModel);

            promptWindow.DataContext = viewModel;
            viewModel.Window = promptWindow;

            promptWindow.Loaded += (s, e) => promptWindow.CenterInWindow(value.Window);

            promptWindow.ShowDialog();
        }

        /// <summary>
        /// Populate the window and view model
        /// </summary>
        /// <param name="window">the window to populate</param>
        /// <param name="viewModel">the view model to populate</param>
        private void Populate(UserNamePasswordPrompt window, UserNamePasswordViewModel viewModel)
        {
            ServerSettingsData settings = this.serverSettingsRepository.ReadSettings();
            viewModel.UserName = settings.UserName;
            window.Password = settings.Password;
        }
    }
}
